//
//  main.m
//  5-12学习
//
//  Created by 钟纪旺 on 16/5/12.
//  Copyright © 2016年 zjw. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Information.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
        NSString * path = @"/Users/zhongjiwang/Desktop/cbv3.vcf";
        
        NSError *err;
        NSString *vcf =[[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
        if (err !=0) {
            NSLog(@"%@",err);
            
            
            
        }
        NSArray *arr =[vcf componentsSeparatedByString:@"\n"];
        NSMutableArray *abc = [NSMutableArray new];
        NSMutableDictionary * diti;
        
        for (NSString *lien in arr) {
            if ([lien hasPrefix:@"BEGIN"]) {
                NSLog(@"开始读取");
                diti =[NSMutableDictionary new];
            }if ([lien hasPrefix:@"END"]) {
                NSLog(@"结束读取");
                [abc addObject:diti];
                
            }if ([lien hasPrefix:@"TEL"]) {
                NSArray *tels = [lien componentsSeparatedByString:@":"];
                
                NSString *last = tels.lastObject;
               // NSMutableArray *str =[NSMutableArray new];
//                [str addObject:last];
                
                //NSLog(@"tel:%@",last);
                diti[@"tel"] = last;
               
               // [diti setObject:last forKey:@"tel:"];
                
             
                
                
            }if ([lien hasPrefix:@"FN"]) {
                NSArray *names = [lien componentsSeparatedByString:@":"];
                NSString *name =names.lastObject;
                name = [name stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
                name = name.stringByRemovingPercentEncoding;
                
                NSLog(@"名字:%@",name);
                diti[@"name"]= name;
                
                
            }
            
            
        }NSLog(@"%@",abc);
        [abc writeToFile:@"/Users/zhongjiwang/Desktop/abc2.txt" atomically:NO];
        NSData *data = [NSJSONSerialization dataWithJSONObject:abc options:NSJSONWritingPrettyPrinted error:nil];
        [data writeToFile:@"/Users/zhongjiwang/Desktop/abc.txt" atomically:NO];
        
        
        NSString *str =[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@",str);
        
        NSMutableArray *res =[NSMutableArray new];
        for (NSDictionary * diti in abc) {
            Information *p =[Information new];
            [p setValuesForKeysWithDictionary:diti];
            [res addObject:p];
        }
        NSLog(@"%@",res);
   }
    return 0;
    
    
    
}
